#include <bits/stdc++.h>
#include <string>
using namespace std;

struct edge
{
    int b; // 起点
    int e; // 终点
    int l; // 边长
};

int f[1001]; // 祖宗

int findF(int n) // 寻找n的祖宗
{
    if (n == f[n])
        return n;
    else
    {
        f[n] = findF(f[n]);
        return f[n];
    }
}

int mergeF(int n, int m) // 合并祖宗
{
    int f_n = findF(n), f_m = findF(m);
    if (f_n == f_m)
        return 0;
    else
    {
        f[f_m] = f_n;
        return 1;
    }
}

bool cmp(struct edge a, struct edge b)
{
    return a.l < b.l;
}
int main()
{
    int n, m, b, e, l;
    cin >> n >> m;
    struct edge list[m];
    for (int i = 1; i <= n; i++)
        f[i] = i;
    for (int i = 0; i < m; i++)
    {
        cin >> b >> e >> l;
        list[i].b = b;
        list[i].e = e;
        list[i].l = l;
    }
    sort(list, list + m, cmp);
    int count = 0, weight = 0; // count: 目前以及选取了几条边  weight: 建路成本
    for (int i = 0; i < m; i++)
    {
        if (mergeF(list[i].b, list[i].e))
        {
            weight += list[i].l;
            count++;
        }

        if (count == n - 1)
            break;
    }
    cout << weight;
    return 0;
}

/**
6 9
2 4 11
3 5 13
4 6 3
5 6 4
2 3 6
4 5 7
1 2 1
3 4 9
1 3 2
 */